<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .fdj {
            width: 1200px;
            display: flex;
            margin: auto;
        }

        .top {
            width: 300px;
            height: 300px;
            position: relative;
        }

        .left img {
            width: 300px;
            height: 300px;
        }

        .cover {
            width: 100px;
            height: 100px;
            background-color: rgba(0, 0, 0, .5);
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;

            /* 不然成为目标源 */
            pointer-events: none;
        }

        .right {
            width: 400px;
            height: 400px;
            overflow: hidden;
            position: relative;
            margin-left: 150px;
            /* opacity: 0; */
        }

        .right img {
            width: 1200px;
            height: 1200px;
            position: absolute;
        }

        .imgs {
            display: flex;
        }

        .imgs img {
            width: 60px;
            height: 60px;
            margin: 10px;
        }
    </style>
</head>

<body>
    <div class="fdj">
        <div class="left">
            <div class="top">
                <img src="" alt="">
                <div class="cover"></div>
            </div>
            <div class="imgs">
                <!-- <img src="" alt=""> -->
            </div>
        </div>
        <div class="right">
            <img src="" alt="">
        </div>
    </div>


    <script>

        // 放大镜 
        //    css部分
        //       左边图片的大小 / 遮罩层的大小的比例  =  右边大图的大小 / 盒子的大小
        //       隐藏元素需要用  visibility:hidden / opacity:0    不能使用display:none

        //    js部分
        //      遮罩层和大图的移动方向是相反的，且成比例
        //         大图的移动范围 / 遮罩层的移动范围

        var oFdj = document.querySelector('.fdj');

        var oLeft = oFdj.querySelector('.left');
        var oLeftImg = oFdj.querySelector('.top img');
        var oCover = oFdj.querySelector('.cover');
        var oRight = oFdj.querySelector('.right');
        var oRightImg = oFdj.querySelector('.right img');
        var oImgs = oFdj.querySelector('.imgs');



        var r = oCover.offsetWidth / 2;
        var maxX = oLeft.clientWidth - oCover.offsetWidth;
        var maxY = oLeft.clientHeight - oCover.offsetHeight;

        console.log(oRightImg);
        var rightMaxX = oRightImg.clientWidth - oRight.offsetWidth;
        var rightMaxY = oRightImg.clientHeight - oRight.offsetHeight;



        var s, b;

        // 数据模拟异步请求
        setTimeout(function () {
            s = ['../images/girlsmall1.jpg', '../images/girlsmall2.jpg', '../images/girlsmall3.jpg', '../images/girlsmall4.jpg'];
            b = ['../images/girlbig1.jpg', '../images/girlbig2.jpg', '../images/girlbig3.jpg', '../images/girlbig4.jpg'];


            // 默认显示第一张
            oLeftImg.src = s[0];
            oRightImg.src = b[0];

            // 显示所有的小图
            var html = '';
            s.forEach(function (v, i) {
                // index 是直接给标签添加
                html += `<img src="${v}" alt="" index=${i}>`
            })
            oImgs.innerHTML = html;


            // 重新给对象添加下标
            // var smallImgs = oImgs.querySelectorAll('img');
            // for (var i = 0; i < smallImgs.length; i++) {
            //     smallImgs[i].index = i;
            // }

        })



        // top绑定鼠标移入移出事件




        // 小图切换
        // var smallImgs = oImgs.querySelectorAll('img');
        // console.log(smallImgs);   // 拿不到的   异步

        // 事件委托
        oImgs.onmouseover = function (e) {
            var target = e.target || e.srcElement;
            if (target.tagName === 'IMG') {
                oLeftImg.src = target.src;
                // oRightImg.src = b[target.index];
                console.log(oRightImg);
                oRightImg.src = b[target.getAttribute('index')];
            }
        }

    </script>




</body>

</html>